package com.hotel.api.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hotel.api.entity.Post;
import com.hotel.api.entity.PostLike;
import com.hotel.api.service.PostService;
import com.hotel.api.utils.JSONResult;
import com.hotel.api.utils.JsonUtils;
import com.hotel.api.utils.UploadFile;
import com.hotel.api.vo.UserPostVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 帖子操作Controller
 *
 * @Author 涛涛
 * @Create 2019/8/29
 */
@RestController
@RequestMapping("/post")
public class PostController {

    @Autowired
    PostService postService;


    /**
     * 用户发帖
     *
     * @param uid
     * @param text
     * @param photo
     * @return
     */
    @ApiOperation("用户发帖")
    @PostMapping("/createPost")
    public JSONResult createPost(Integer uid, String text,
                                 MultipartFile photo) {
        Post post = new Post();
        if (uid == null) {
            System.out.println("用户id为空");
            return JSONResult.build(400, "发帖失败", null);
        }
        if (text == null) {
            System.out.println("正文id为空");
            return JSONResult.build(400, "请输入内容", null);
        }
//        if (photo == null) {
//            return JSONResult.build(400,"后台接收不到图片",null);
//        }
//        else {
//            String photoName = UploadFile.getFilePath(photo);
//            post.setPhoto(photoName);
//        }
        post.setUid(uid);
        post.setText(text);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // new Date()为获取当前系统时间，也可使用当前时间戳
        String date = df.format(new Date());
        post.setCreateTime(date);
        post.setDelState((byte) 0);
        postService.insert(post);
        System.out.println("发帖成功");
        return JSONResult.ok("发送成功");

    }

    /**
     * 用户查看所有帖子
     *
     * @return
     */
    @ApiOperation("用户查看所有帖子")
    @PostMapping("/userScanPost")
    public JSONResult userScanPost(Integer uid, @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum) {
        System.out.println("有人逛朋友圈了" + new Date());
        PageHelper.startPage(pageNum, 10);
        List<UserPostVO> list = postService.userScanPost(uid);
        PageInfo<UserPostVO> pageInfo = new PageInfo<>(list);
        if (list == null) {
            return JSONResult.errorException("暂无动态");
        } else {
            return JSONResult.ok(pageInfo);
        }
    }

    /**
     * 管理员查看所有贴子
     *
     * @return
     */
    @ApiOperation("管理员查看所有贴子")
    @GetMapping("/selectAllPost")
    public JSONResult selectAllPost(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                                    @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit) {
        PageHelper.startPage(page, 10);
        List<Post> list = postService.selectAll();
        PageInfo<Post> pageInfo = new PageInfo<>(list);
        if (list != null) {
            return JSONResult.build(200, "查询成功", pageInfo);
        } else {
            return JSONResult.errorMsg("暂无帖子");
        }
    }


    /**
     * 管理员删除帖子/用户删除自己的帖子
     *
     * @param id
     * @return
     */
    @ApiOperation("管理员删除帖子/用户删除自己的帖子")
    @PostMapping("/deleteByPostId")
    public JSONResult deleteByPostId(Integer id) {
        int i = postService.deletePostById(id);
        if (i == 0) {
            return JSONResult.errorException("删除失败");
        } else {
            return JSONResult.build(200, "删除成功", "");
        }
    }

    /**
     * 用户查看自己的帖子
     *
     * @param id
     * @return
     */

    @ApiOperation("用户查看自己的帖子")
    @PostMapping("/selectUserPost")
    public JSONResult selectUserPost(Integer id) {
        List list = postService.selectUserPost(id);

        if (list == null) {
            return JSONResult.errorException("您没发过帖");
        } else {
            return JSONResult.build(200, "查询成功", list);
        }
    }

    /**
     * 用户点赞
     *
     * @param postLike
     * @return
     */
    @ApiOperation("用户点赞")
    @PostMapping("/userAddLike")
    public JSONResult userAddLike(PostLike postLike) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // new Date()为获取当前系统时间，也可使用当前时间戳
        String date = df.format(new Date());
        postLike.setOperateTime(date);
        postLike.setState(1);
        int i = postService.insert(postLike);
        if (i == 0) {
            return JSONResult.errorMsg("点赞失败");
        } else {
            return JSONResult.ok("点赞成功");
        }
    }

    /**
     * 用户取消赞
     *
     * @param postLike
     * @return
     */

    @ApiOperation("用户取消赞")
    @PostMapping("/userCancelLike")
    public JSONResult userCancelLike(PostLike postLike) {
        int i = postService.userCancelLike(postLike.getUid(), postLike.getPid());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // new Date()为获取当前系统时间，也可使用当前时间戳
        String date = df.format(new Date());
        postLike.setOperateTime(date);
        if (i == 0) {
            return JSONResult.errorMsg("取消失败");
        } else {
            return JSONResult.ok("取消成功");
        }
    }
}
